% REF2REF - transform reflectance to reflectance
%
% Transformation from reflectance to reflectance with different spacing or
% relative reflectance by user selected interpolating methods.
%
% Usage:
%     ret_spec = ref2ref( ...
%       src_spec, ...
%       fmin_lambda,...
%       finterval,...
%       fmax_lambda,...
%       omin_lambda, ...
%       ointerval,...
%       omax_lambda )
%
% Arguments:
%    src_spec - the source reflectance (spectrum)
%    fmin_lambda - input minimal wavelength in nm
%    finterval - the interval wavelength incremental in nm
%    fmax_lambda - input maximal wavelength in nm
%    omin_lambda - output minimal wavelength in nm
%    ointerval - the interval wavelength incremental in nm
%    omax_lambda - output maximal wavelength in nm
%
% Example
%
%     spec = dlmread('../DataMeasure/PrinterInformation/CMYKRGBLinearRef.txt');
%     ret_spec = color_tool.ref2ref(...
%         spec, ...
%         380, 5, 730,...
%         300, 10, 700);

function ret_spec = ref2ref( ...
    src_spec, ...
    fmin_lambda,...
    finterval,...
    fmax_lambda,...
    omin_lambda, ...
    ointerval,...
    omax_lambda )

%    ret_spec = src_spec;
% 
%     if fmin_lambda > omin_lambda || fmax_lambda > omax_lambda || finterval ~= ointerval
%         front_truncate = (omin_lambda-fmin_lambda)./ finterval;
%         end_truncate = (fmax_lambda-omax_lambda)./finterval;
%         interval = ointerval./finterval;
%         if end_truncate == 0
%             ret_spec = ret_spec(:, (front_truncate+1):interval:end );
%         else
%             ret_spec = ret_spec(:, (front_truncate+1):interval:(end-end_truncate) );
%         end
%     end

    out_rows = size(src_spec,1);
    out_cols = size(omin_lambda:ointerval:omax_lambda,2);
    ret_spec = zeros(out_rows, out_cols);

    for i=1:out_rows
        ret_spec(i,:) = interp1(...
            fmin_lambda:finterval:fmax_lambda,...
            src_spec(i,:),...
            omin_lambda:ointerval:omax_lambda,...
            'linear', 'extrap');
    end

